/*
 * lexerkeyvariable.c
 *
 *  Created on: 2013-5-8
 *      Author: xkaying
 */

#include "lexer.h"
#include "lexerpackage.h"
#include "lexerimport.h"
#include "lexerkeyvariable.h"

#include "../typedef/token.h"

#include <assert.h>
#include <stdio.h>

char *LexerKeyVariable(char *ch, int *line, int *len, Lexer *plex, int size)
{
	assert(ch);
	assert(line);
	assert(len);
	assert(plex);
	//////////先读取,然后判断,是否是关键字,否则都是变量
	int state;
	state = 0;
	Token *ptoken;
	ptoken = 0;
	while ((*len) < size) {
		switch (state)
		{
			case 0: {
				if (((*ch >= 'a') && ((*ch) <= 'z'))
						|| ((*ch >= 'A') && ((*ch) <= 'Z')) || ((*ch) == '_')) {
					ptoken = TokenInit(TokenType_Variable);
					ptoken->str = ch;
					ptoken->len++;
					/////////////
					(*len)++;
					ch++;
					state = 1;
					continue;
				}
				return ch;
			}
			case 1: {
				if (((*ch >= 'a') && ((*ch) <= 'z'))
						|| ((*ch >= 'A') && ((*ch) <= 'Z')) || ((*ch) == '_')) {
					ptoken->len++;
					/////////////
					(*len)++;
					ch++;
					continue;
				}
				if (((*ch >= '0') && ((*ch) <= '9'))) {
					ptoken->len++;
					/////////////
					(*len)++;
					ch++;
					continue;
				}
				//不是上述字符,表示变量读取结束,判断是否为关键字

				TokenKeyCheck(ptoken);
				LexerPush(plex, ptoken);
				// 如果是package,需要进行特殊处理
				if (ptoken->type == TokenKey_PACKAGE) {
					return LexerPackage(ch, line, len, plex, size);
				}
				//如果是import 需要进行特殊处理
				if (ptoken->type == TokenKey_IMPORT) {
					return LexerImport(ch, line, len, plex, size);
				}
				return ch;
			}
			default: {
				printf("lexer key variable 状态机错误.\n");
				return 0;
			}
		}
	}
	if (ptoken != 0) {
		LexerPush(plex, ptoken);
		ptoken = 0;
	}
	return ch;
}
